<?php
class disk
{

    public function __construct ()
    {
        $this->db = new DB();
    }

    public function Update ($data)
    {
        if (!$this->Check_disk($data))
        {
            return False;
        }
        $qry = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `id`='" . $data['id'] . "' LIMIT 1");
        $this->rlt = mysql_fetch_array($qry, MYSQL_ASSOC);
        if (array_diff_assoc($data, $this->rlt))
        {
            $ch_disk = array("disk1" => $data['disk1'], "disk2" => $data['disk2'], "disk3" => $data['disk3'], "disk4" => $data['disk4']);
            $ch_disk0 = array_filter($ch_disk, "ary_filter0");
            $ch_disk1 = array_filter($ch_disk, "ary_filter1");
            if (!empty($ch_disk0))
            {
                $this->Upd_canecl($ch_disk0, $data);
            }
            if (!empty($ch_disk1))
            {
                $this->Upd_select($ch_disk1, $data);
            }
        }
        else
        {
            return FALSE;
        }
    }

    public function Browser ($id)
    {
        $qry = $this->db->query("SELECT `disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `id`='" . $id . "' limit 1");
        $rlt = mysql_fetch_array($qry, MYSQL_ASSOC);
        return $rlt;
    }

    public function Check_disk ($data)
    {
        if ($data['prev_id'] == "0")
        {
            return TRUE;
        }
        $qry = $this->db->query("SELECT `disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `id`='" . $data['prev_id'] . "' limit 1");
        $rlt = mysql_fetch_array($qry, MYSQL_NUM);
        $pdata = array($data['disk1'], $data['disk2'], $data['disk3'], $data['disk4']);
        if (in_array("0", array_diff_assoc($rlt, $pdata)))
        {
            return FALSE;
        }
        return TRUE;
    }

    public function Upd_canecl ($ch_disk, $data)
    {
        $dk_str = "";
        foreach ($ch_disk as $key => $val)
        {
            $dk_str = trim($dk_str . "," . $key . "=" . "'{$val}'", ",");
        }
        if (isset($data['acl']))
        {
            switch ($data['acl'])
            {
                case 255 :
                    break;
                case 5 :
                    $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `id`='" . $data['id'] . "' OR `prev_id`='" . $data['id'] . "'");
                    $qry3 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $data['id'] . "'");
                    while ($rlt3 = mysql_fetch_array($qry3))
                    {
                        $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt3['id'] . "'");
                        $qry2 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $rlt3['id'] . "'");
                        while ($rlt2 = mysql_fetch_array($qry2))
                        {
                            $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt2['id'] . "'");
                            $qry1 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $rlt2['id'] . "'");
                            while ($rlt1 = mysql_fetch_array($qry1))
                            {
                                $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt1['id'] . "'");
                            }
                        }
                    }
                    break;
                case 4 :
                    $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `id`='" . $data['id'] . "' OR `prev_id`='" . $data['id'] . "'");
                    $qry2 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $data['id'] . "'");
                    while ($rlt2 = mysql_fetch_array($qry2))
                    {
                        $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt2['id'] . "'");
                        $qry1 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $rlt2['id'] . "'");
                        while ($rlt1 = mysql_fetch_array($qry1))
                        {
                            $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt1['id'] . "'");
                        }
                    }
                    break;
                case 3 :
                    $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `id`='" . $data['id'] . "' OR `prev_id`='" . $data['id'] . "'");
                    $qry1 = $this->db->query("SELECT `id`,`acl`,`prev_id`,`disk1`,`disk2`,`disk3`,`disk4` FROM `k_user` WHERE `prev_id`='" . $data['id'] . "'");
                    while ($rlt1 = mysql_fetch_array($qry1))
                    {
                        $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `prev_id`='" . $rlt1['id'] . "'");
                    }
                    break;
                case 2 :
                    $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `id`='" . $data['id'] . "' OR `prev_id`='" . $data['id'] . "'");
                    break;
                case 1 :
                    return TRUE;
                    break;
            }
            return TRUE;
        }
    }

    public function Upd_select ($ch_disk, $data)
    {
        $dk_str = "";
        foreach ($ch_disk as $key => $val)
        {
            $dk_str = trim($dk_str . "," . $key . "=" . "'{$val}'", ",");
        }
        $this->db->query("UPDATE `k_user` SET {$dk_str} WHERE `id`='" . $data['id'] . "'");
    }

    public function trans_on ($val)
    {
        if ($val == "on")
        {
            return "1";
        }
        return "0";
    }

}

function ary_filter1 ($val)
{
    if ($val === "1")
    {
        return true;
    }
    return false;
}

function ary_filter0 ($val)
{
    if ($val === "0")
    {
        return true;
    }
    return false;
}

?>